<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/10-15:46
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\model\Roles;
    use app\admin\service\MenuService;
    use app\admin\service\RoleService;

    use Jrk\Trees;
    use think\Validate;

    class Role extends AdminBase
    {
        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
            $this->service=new RoleService();
            $this->model=new Roles();
            $this->del="权限组";
        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addRole
         * @describe:
         */
        public function addRole(){
            if(IS_AJAX) {
                $data     = $this->request->post();
                $validate = Validate::make([
                    'name'  => 'require|chsAlpha|max:25',
                    'rule' => 'require'
                ]);

                if(!$validate->check($data)) {
                    $this->error('提交失败：'.$validate->getError());
                }

                if (!isset($data['id'])){
                    $info=$this->service->getOne(['name'=>"".$data['name'].""]);

                    if ($info){
                        $this->error("该角色名称已存在");
                    }
                }

                $data['rule'] = serialize($data['rule']); //数组转字符串

                if (isset($data['id'])){
                    addLog("角色权限修改操作","修改角色权限：".$data['name']);
                }else{
                    addLog("角色权限添加操作","新增角色权限：".$data['name']);
                }

                return $this->model->addAndEdit($data); //添加数据

            }

            return $this->fetch();

        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/10
         * @name: editRole
         * @describe:编辑
         */
        public function editRole(){
            $id   = $this->request->param("id/d");//父id
            $info = $this->service->getOne(['id'=>$id]);
            if (empty($info)){
                $this->assign('msg','当前id:'.$id.'错误');
                $this->fetch("public/404");
            }
            $this->assign(compact( "info"));
            return $this->fetch();
        }



        /**
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/10
         * @name: getRoles
         * @describe:角色权限组获取
         */
        public function getRoles(){
            if (IS_AJAX){
                $id  = $this->request->param("id/d");

                if ($id==1){ //超级管理员
                    $data=MenuService::menuList();
                    $list=Trees::DeepTree($data,"list");
                }else{

                    if ($id==0){
                        $data=MenuService::menuList();
                        $list=Trees::DeepTree($data,"list");

                    } else{
                        $info=$this->service->getOne(['id'=>$id]); //
                        $rules=$info['rule'];
                        if (isset($rules) && !empty($rules)){
                            $ids=unserialize($info['rule']);
                            $data=MenuService::menuList();

                            if(!empty($data)) {
                                foreach($data as $k => $v){
                                    if(in_array($v['id'], $ids)) { //
                                        $data[$k]['checked'] = true; //追加默认选中
                                        //$da[$k]['disable']=true; //不可选择
                                    }
                                }
                            }
                            $list=Trees::DeepTree($data,"list");
                        }else{
                            $data=MenuService::menuList();
                            $list=Trees::DeepTree($data,"list");
                        }

                    }
                }
                $this->success("ok", '', $list);
            }
        }




    }